package com.koc.robot;


public class BackgroundBlock
{
	public BackgroundBlock()
	{
		this.x = 0;
		this.y = 0;
		this.width = 0;
		this.height = 0;
		this.dir = Dir.hor;
		this.speed = 0;
		this.r = 0;
	}
	
	public BackgroundBlock(float x, float y, float width, float height, Dir dir, float speed)
	{
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.dir = dir;
		this.speed = speed;
		this.r = 0;
	}
	
	public void update(float dt)
	{
		if (dir == Dir.hor)
		{
			x = (int) (x + speed * dt);
			if (x < -Global.screenwidth - width || x > Global.screenwidth * 2 + width)
			{
				resetbox(false);
			}
		}
		else
		{
			y = (int)(y + speed * dt);
			if (y < -Global.screenheight - height || y > Global.screenheight * 2 + height)
			{
				resetbox(false);
			}
		}
	}
	
	public void draw()
	{
		if (x + width >= -MenuState.menuoffsetx && x < -MenuState.menuoffsetx + Global.screenwidth && y + height >= -MenuState.menuoffset && y < -MenuState.menuoffset + Global.screenheight)
		{
			Color fillColor = new Color();
			fillColor.R = Global.fillcolor.R;
			fillColor.G = Global.fillcolor.G;
			fillColor.B = Global.fillcolor.B;
			if (dir == Dir.hor) 
			{
				fillColor.R = fillColor.R * (1 - (float)(width) / 100);
				fillColor.G = fillColor.G * (1 - (float)(width) / 100);
				fillColor.B = fillColor.B * (1 - (float)(width) / 100);
			}
			else
			{
				fillColor.R = fillColor.R * (1 - (float)(height) / 100);
				fillColor.G = fillColor.G * (1 - (float)(height) / 100);
				fillColor.B = fillColor.B * (1 - (float)(height) / 100);
			}
			//Util.drawblock(x + MenuState.menuoffsetx, y + MenuState.menuoffset, width, height, fillColor, Global.outlinecolor, r);
			Global.fillblock(x + MenuState.menuoffsetx, y + MenuState.menuoffset, width, height, fillColor.R, fillColor.G, fillColor.B);
		}
	}
	
	public void resetbox(boolean first)
	{
		int rand = Util.random(1, 5);
		dir = Dir.hor;
		if (rand % 2 == 1) 
		{
			dir = Dir.ver;
		}
		
		speed = Util.random(300, 400);
		if (dir == Dir.hor)
		{
			width = Util.random(30, 100);
			//height = Util.random(10, 20);
			height = Util.random(20, 40);
			if (first)
			{
				x = Util.random((int)(-Global.screenwidth - width), Global.screenwidth * 2);
			}
			else
			{
				x = -Global.screenwidth - width;
			}
			y = Util.random(-Global.screenheight, Global.screenheight * 2);
			if (rand / 2 == 2) 
			{
				if (first)
				{
					x = Util.random(-Global.screenwidth, Global.screenwidth * 2);
				}
				else
				{
					x = Global.screenwidth * 2 + width;
				}
				speed = -speed;
			}
		}
		else
		{
			//width = Util.random(10, 20);
			width = Util.random(20, 40);
			height = Util.random(30, 100);
			if (first)
			{
				y = Util.random(-Global.screenheight, Global.screenheight * 2);
			}
			else
			{
				y = -Global.screenheight - height;
			}
			x = Util.random(-Global.screenwidth, Global.screenwidth * 2);
			if (Float.compare((float)(rand) / 2, 1.5f) == 0)
			{
				if (first)
				{
					y = Util.random(-Global.screenheight, Global.screenheight * 2);
				}
				else
				{
					y = Global.screenheight * 2 + height;
				}
				speed = -speed; 
			}
		}
	}
	
	private float x;
	private float y;
	private float width;
	private float height;
	private Dir dir;
	private float speed;
	private float r;
}
